<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input v-model="listQuery.code" placeholder="股票编码" style="width: 200px;" class="filter-item" />
      <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" @click="getList">
        查询
      </el-button>
    </div>
    <el-table :data="list" style="width: 100%;padding-top: 15px;">
      <el-table-column label="ID" prop="id"  align="center" >
        <template  scope="scope">
          <span>{{scope.$index+1}}</span>
        </template>
      </el-table-column>
      <el-table-column label="时间"  align="center">
        <template slot-scope="{row}">
          <span>{{ row.date }}</span>
        </template>
      </el-table-column>
      <el-table-column label="股票编码"  align="center">
        <template slot-scope="{row}">
          <span>{{ row.code }}</span>
        </template>
      </el-table-column>
      <el-table-column width="100px" label="推荐指数" align="center">
        <template slot-scope="{row}">
          <svg-icon v-for="n in + (1,row.star)" :key="n" icon-class="star" class="icon-star" />
        </template>
      </el-table-column>
      <el-table-column label="指数操作" align="center" class-name="small-padding fixed-width" >
        <template slot-scope="{row}">
          <el-button type="primary" size="mini" @click="handleStar(row.id,-1)">
            -
          </el-button>
          <el-button size="mini" type="danger" @click="handleStar(row.id,1)">
              +
          </el-button>
        </template>
      </el-table-column>

      <el-table-column label="说明"  align="center">
        <template slot-scope="{row}">
          <span>{{ row.remark}}</span>
        </template>
      </el-table-column>
      <el-table-column label="计算周期（天）"  align="center">
        <template slot-scope="{row}">
          <span>{{ row.cycle }}</span>
        </template>
      </el-table-column>
      <el-table-column label="计算时间"  align="center">
        <template slot-scope="{row}">
          <span>{{ row.createDate }}</span>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.size" @pagination="getList" />
  </div>
</template>

<script>
import waves from '@/directive/waves' // waves directive
import { parseTime } from '@/utils'
import Pagination from '@/components/Pagination' // secondary package based on el-pagination
import { strategy, handleStar } from '@/api/strategy'
export default {
  name: 'ComplexTable',
  components: { Pagination },
  directives: { waves },
  data() {
    return {
      tableKey: 0,
      list: null,
      total: 0,
      listLoading: true,
      listQuery: {
        page: 1,
        size: 30,
        code: null,
        cycle: 1
      },
      temp: {
        id: undefined,
        importance: 1,
        remark: '',
        timestamp: new Date(),
        title: '',
        type: '',
        status: 'published'
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      strategy(this.listQuery).then(response => {
        this.list = response.data.content
        this.total = response.data.totalElements
      })
    },
    handleStar(id,star) {
      handleStar({id,star}).then(response => {
        if (response.code===0){
          this.getList();
        }
      })
    }
  }
}
</script>
